package com.tianqu.tms.www.member.controller;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.security.TmsUserProfile;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.Member;
import com.tianqu.tms.entity.User;
import com.tianqu.tms.www.member.viewhelper.MemberInputViewHelper;

/**
 * 
 * 会员帐号状态修改结果页
 * 
 * @author tianshifu
 * 
 */
public class MemberUDLoginStatusResultController extends TmsController {

	private static final String ZC = "正常";

	private static final String JY = "禁用";

	private Logger logger = Logger
			.getLogger(MemberUDLoginStatusResultController.class);

	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controller = TxController.getTxController();

		// 添加事务提交异常捕获
		try {
			controller.begin();
			// 从uc中获取状态信息，先查出userID再在用户表中更新用户状态
			MemberInputViewHelper view = (MemberInputViewHelper) uc
					.getAttribute(TmsConstants.VIEWHELP);
			Member member = Member.getInstance();
			member = member.findMemberInfoByMemId(view.getMemberId());

			User user = new User();

			// 重构下用户状态，供前台修改页面radio选中
			if (view.getMemStatus().equals(ZC)) {
				user.setFlag(3);
			}
			if (view.getMemStatus().equals(JY)) {
				user.setFlag(1);
			}
			user.setLoginId(member.getUserId());
			TmsUserProfile profile = uc.getUserProfile();
			user.setUpdateUserId(profile.getUniqueId());
			// 更新用户表信息
			user.updateLoginStatusByUserId(user);

			logger.debug(this.getClass().getName() + "#execute was completed.");
			controller.commit();
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}

	}

}
